@echo off
title Email Demo System

echo.
echo ========================================
echo Email Demo System
echo ========================================
echo.

REM Check Python installation
python --version >nul 2>&1
if errorlevel 1 (
    echo ERROR: Python not found, please install Python 3.7+
    pause
    exit /b 1
)

REM Check if virtual environment exists
if not exist "venv" (
    echo Creating virtual environment...
    python -m venv venv
    if errorlevel 1 (
        echo ERROR: Failed to create virtual environment
        pause
        exit /b 1
    )
)

REM Activate virtual environment
echo Activating virtual environment...
call venv\Scripts\activate.bat

REM Check if requirements.txt exists
if not exist "requirements.txt" (
    echo ERROR: requirements.txt not found
    pause
    exit /b 1
)

REM Install dependencies
echo Installing dependencies...
pip install -r requirements.txt
if errorlevel 1 (
    echo ERROR: Failed to install dependencies
    pause
    exit /b 1
)

REM Check if .env file exists, copy example if not
if not exist ".env" (
    if exist ".env.example" (
        echo Copying environment configuration...
        copy ".env.example" ".env" >nul
        echo Please edit .env file to configure your environment variables
    )
)

REM Start application
echo.
echo Starting application...
echo.
python run.py

REM If program exits abnormally, pause to view error messages
if errorlevel 1 (
    echo.
    echo ERROR: Application failed to start, please check error messages
    pause
)

echo.
echo Application stopped
pause